import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../components/Login.vue'
import home from '../components/home.vue'
import welcome from '../components/welcome.vue'
import users from '../components/user/users.vue'
import rights from '../components/power/rights.vue'
import roles from '../components/power/roles.vue'
import goods from '../components/good/goods.vue'
import categories from '../components/good/categories.vue'
import params from '../components/good/params.vue'
import orders from '../components/orders/orders.vue'
import reports from '../components/reports/reports.vue'
import add from '../components/good/add.vue'

Vue.use(VueRouter)



const router = new VueRouter({
  routes: [
    { path: '/', redirect: '/login' },
    { path: '/login', component: Login },
    {
      path: '/home', component: home, redirect: '/welcome', children: [
        { path: '/welcome', component: welcome },
        { path: '/users', component: users },
        { path: '/rights', component: rights },
        { path: '/roles', component: roles },
        { path: '/goods', component: goods },
        { path: '/goods/add', component: add },
        { path: '/categories', component: categories },
        { path: '/params', component: params },
        { path: '/orders', component: orders },
        { path: '/reports', component: reports },
      ]
    },

  ]
})

// 挂载路由导航守卫
router.beforeEach((to, from, next) => {
  // to 将要访问的路径
  // from代表从哪个路径跳转而来
  // next 是一个函数，表示放行  next() 放行  next('/login')强制跳转到login

  // 如果用户访问的是登录页就直接放行
  if (to.path === '/login') return next();
  // 从sessionStorage里面取出token
  const tokenStr = window.sessionStorage.getItem('token');
  // 如果没有token值那就强制跳转到login页面
  if (!tokenStr) return next('/login');
  next();
})

export default router
